package com.hxminer.hxwebs.service;


import com.hxminer.hxwebs.dao.UserDao;
import com.hxminer.hxwebs.enmus.ResultEnum;
import com.hxminer.hxwebs.exceptions.UserException;
import com.hxminer.hxwebs.model.ReUser;
import com.hxminer.hxwebs.model.Result;
import com.hxminer.hxwebs.model.User;
import com.hxminer.hxwebs.repository.LoginRepository;
import com.hxminer.hxwebs.utils.ResultUitl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * Author YRG
 * Created by on 2018/5/9.
 * Describe
 */
@Service
public class LoginService {

    private final static Logger logger = LoggerFactory.getLogger(LoginService.class);
    @Autowired
    UserDao userDao;

    @Autowired
    LoginRepository loginRepository;


    Map<String, ReUser> userMap = new HashMap<>();

    @Transactional
    public Result<User> findByUser(User user, HttpSession httpSession) throws Exception {

        if ("".equals(user.getUsername())) {
            throw new UserException(ResultEnum.USERNAME_OR_PASSWROD);
        }
        if ("".equals(user.getPassword())) {
            throw new UserException(ResultEnum.USERNAME_OR_PASSWROD);
        }

        List<User> users = userDao.findByUsernameAndPassword(user.getUsername(), user.getPassword());

        if (users.size() <= 0 || users == null) {
            throw new UserException(ResultEnum.ACOUNT_NOT);
        } else {
            User user1 = users.get(0);
            String u = UUID.randomUUID().toString();
            ReUser reUser = new ReUser();
            reUser.setToken(u);
            reUser.setUsername(user1.getUsername());
            reUser.setAddress(user1.getAddress());
            reUser.setPhoneMail(user1.getPhonemail());
            userMap.put(u, reUser);
            return ResultUitl.success(reUser, "登录成功!");
        }
    }

    @Transactional
    public Result<User> registerByUser(User user) {

        if ("".equals(user.getUsername())) {
            throw new UserException(ResultEnum.USERNAME_OR_PASSWROD);
        }
        if ("".equals(user.getPassword())) {
            throw new UserException(ResultEnum.USERNAME_OR_PASSWROD);
        }
        if ("".equals(user.getPhonemail())) {
            throw new UserException(ResultEnum.PHONE_OR_MAIL);
        }
        List<User> users1 = userDao.findByUsername(user.getUsername());
        List<User> users2 = userDao.findByPhonemail(user.getUsername());

        if ((users1 != null && users1.size() > 0) || (users2 != null && users2.size() > 0)) {
            throw new UserException(ResultEnum.USER_EXISTS);
        } else {
            loginRepository.save(user);
            return ResultUitl.success(ResultEnum.REGISTER_SUCCESS, user);
        }
    }


    /**
     * 预登录中
     *
     * @param token
     * @return
     */
    @Transactional
    public Result<ReUser> prelLogin(String token) {
        if (token == null || "".equals(token)) {
            return ResultUitl.error(1, "不存在，需要先登入");
        }
        ReUser user = userMap.get(token);
        if (user == null) {
            return ResultUitl.error(1, "不存在，需要先登入");
        }
        ReUser reUser = new ReUser();
        reUser.setToken(token);
        reUser.setUsername(user.getUsername());
        reUser.setAddress(user.getAddress());
        reUser.setPhoneMail(user.getPhoneMail());
        return ResultUitl.success(ResultEnum.SUCCESS, user);
    }
}















